﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using PBDesk.Utilities;
using System.Web;
using Umbraco.Framework.Dynamics;
using System.Web.Helpers;
using PBDesk.com.Extension.WebHelper;
using System.Globalization;
using Umbraco.Cms.Web;
using PBDesk.com.Extension.FeedsAggregator;
using PBDesk.com.Extension.FeedsAggregator.Entities;
using PBDesk.com.Extension.Articles;
using Umbraco.Hive;


namespace PBDesk.com.Extension.UmbHelper
{
    public class MyHelper
    {
        private const string DEFAULT_THEME = "Rambling";
        public static string GetTheme()
        {
            string theme = DEFAULT_THEME;
            var themeEnabled = ConfigurationManager.AppSettings["ThemeEnabled"] ?? "false";
            if (themeEnabled.ToLower() == "true")
            {
                theme = HttpContext.Current.Request.QueryString["t"];
                if (theme.IsNullOrEmpty())
                {
                    theme = ConfigurationManager.AppSettings["Theme"] ?? DEFAULT_THEME;
                }
                if (theme.IsNullOrEmpty())
                {
                    theme = DEFAULT_THEME;
                }
            }
            return theme;
        }
        public static string GetBannerColumnStyle(int colCount)
        {
            return (colCount == 1) ? "columns100" : (colCount == 2 ? "columns5050" : "columns");
        }

        public static string GetCanonicalUrl(string url)
        {
            var canonicalUrl = String.Empty;
            if (HttpContext.Current != null)
            {
                string host = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
                if (host.ToLower().StartsWith("www"))
                {
                    canonicalUrl = string.Concat("http://", host, url);
                }
                else
                { canonicalUrl = string.Concat("http://www.", host, url); }
            }
            return canonicalUrl;
        }
        public static string GetCanonicalUrl(BendyObject dynaModel)
        {
            return GetCanonicalUrl(dynaModel.AsDynamic().Url);
        }

        //public static OArticle GetArticleOfTheDay(dynamic dynaModel, int artId = 0)
        //{
        //    return ArticleHelper.GetArticleOfTheDay(dynaModel, artId);
        //}

        public static dynamic GetArticleOfTheDay(dynamic dynaModel, int artId = 0)
        {            
            dynamic artOfDay = null;
            artOfDay = CacheManager.GetArticleOfTheDay();
            if (artOfDay != null)
            {
                return artOfDay;
            }
            if (artId == 0)
            {
                artId = GetCurrentArticleId();
            }
            while (artOfDay == null && artId > 20110101)
            {
                var arts = dynaModel.AncestorsOrSelf.Last().ArticlePages.Where("articleId == @0", artId.ToString());
                if (arts != null)
                {
                    artOfDay = arts.FirstOrDefault();
                }
                artId--;
            }
            return artOfDay;

            
        }
        //public static BendyObject GetArticleOfTheDay(BendyObject dynaModel, int artId = 0)
        //{
        //    BendyObject artOfDay = null;
        //    if (artId == 0)
        //    {
        //        artId = GetCurrentArticleId();
        //    }
        //    while (artOfDay == null)
        //    {
        //        var arts = MyHelper.GetHomeNode(dynaModel).AsDynamic().ArticlePages.Where("articleId == @0", artId.ToString());
        //        if (arts != null)
        //        {
        //            artOfDay = arts.FirstOrDefault();
        //        }
        //        artId--;
        //    }
        //    return artOfDay;
        //}

        private static int GetCurrentArticleId()
        {
            int artid = 0;
            string currentDt = DateTime.Now.ToString("yyyyMMdd");
            artid = currentDt.As<int>();
            return artid;
        }

        //public static BendyObject GetArticleOfTheDay(BendyObject dynaModel, string previewDate = "")
        //{
        //    DateTime previewDateDt = DateTime.Today;
        //    BendyObject artOfDay = null;
        //    if (previewDate.IsNotNullAndNotEmpty() && previewDate.ToLower() != "today")
        //    {
        //        DateTime tempDt;
        //        if (DateTime.TryParseExact(previewDate, "dd/MM/yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out tempDt))
        //        {
        //            previewDateDt = tempDt;
        //        }
        //    }

        //    if (previewDateDt == DateTime.Today)
        //    {
        //        artOfDay = CacheManager.GetArticleOfTheDay();
        //        if (artOfDay != null)
        //            return artOfDay;
        //    }

        //    artOfDay = GetHomeNode(dynaModel).AsDynamic().Descendants.FirstOrDefault("PreviewDate == @0", previewDateDt.ToString("MM/dd/yyyy"));
        //    CacheManager.SetArticleOfTheDay(artOfDay);
        //    return artOfDay;
        //}

        public static BendyObject GetHomeNode(BendyObject dynaModel)
        {            
            var home = CacheManager.GetHomeNode();
            if (home == null)
            {
                home = dynaModel.AsDynamic().AncestorsOrSelf.First("NodeTypeAlias == @0", "HomePage");
                //home = dynaModel.AsDynamic().AncestorsOrSelf.Last();
                //home = dynaModel.AsDynamic().AncestorsOrSelf.Where("NodeTypeAlias == @0", "HomePage").First();
                CacheManager.SetHomeNode(home);                                
            }
            return home;

        }

        public static C9Item GetTWC9()
        {
            var c9Item = CacheManager.GetTWC9Item();
            if (c9Item == null)             
            {
                c9Item = FeedHelper.GetTWC9();
                CacheManager.SetTWC9Item(c9Item);
                
            }
            return c9Item;
        }

        public static List<C9Item> GetTWC9Items(int itemCount = 50)
        {
            return FeedHelper.GetTWC9Items(itemCount);
        }

        public static string GetTagLabels(dynamic tagCollection)
        {
            string tags = string.Empty;
            string tagLinkFormat = " <a href=\"#\">{0}</a>,";
            try
            {
                if (tagCollection != null)
                {
                    foreach (var tag in tagCollection)
                    {
                        tags += string.Format(tagLinkFormat, tag.ToString());
                    }
                    tags = tags.Remove(tags.Length-1, 1);
                }
            }
            catch (Exception ex)
            {
                MyLog.WriteErr<MyHelper>("Error in MyHelper.GetTagLabels()", ex);
            }
            return tags;
        }
       
       

        
    }
}
